package cn.iocoder.yudao.module.system.controller.admin.roleweixinmenu;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.roleweixinmenu.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.roleweixinmenu.RoleWeixinMenuDO;
import cn.iocoder.yudao.module.system.service.roleweixinmenu.RoleWeixinMenuService;

@Tag(name = "管理后台 - 微信菜单和角色关联")
@RestController
@RequestMapping("/system/role-weixin-menu")
@Validated
public class RoleWeixinMenuController {

    @Resource
    private RoleWeixinMenuService roleWeixinMenuService;

    @PostMapping("/create")
    @Operation(summary = "创建微信菜单和角色关联")
//    @PreAuthorize("@ss.hasPermission('system:role-weixin-menu:create')")
    public CommonResult<Long> createRoleWeixinMenu(@Valid @RequestBody RoleWeixinMenuSaveReqVO createReqVO) {
        return success(roleWeixinMenuService.createRoleWeixinMenu(createReqVO));
    }

    @PostMapping("/saveWeinxinMenu")
    @Operation(summary = "创建微信菜单和角色关联")
//    @PreAuthorize("@ss.hasPermission('system:role-weixin-menu:create')")
    public CommonResult<Long> saveWeinxinMenu(@Valid @RequestBody RoleWeixinMenuSaveReqVO2 createReqVO) {
        return success(roleWeixinMenuService.saveWeinxinMenu(createReqVO));
    }


    @PutMapping("/update")
    @Operation(summary = "更新微信菜单和角色关联")
//    @PreAuthorize("@ss.hasPermission('system:role-weixin-menu:update')")
    public CommonResult<Boolean> updateRoleWeixinMenu(@Valid @RequestBody RoleWeixinMenuSaveReqVO updateReqVO) {
        roleWeixinMenuService.updateRoleWeixinMenu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信菜单和角色关联")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('system:role-weixin-menu:delete')")
    public CommonResult<Boolean> deleteRoleWeixinMenu(@RequestParam("id") Long id) {
        roleWeixinMenuService.deleteRoleWeixinMenu(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除微信菜单和角色关联")
//                @PreAuthorize("@ss.hasPermission('system:role-weixin-menu:delete')")
    public CommonResult<Boolean> deleteRoleWeixinMenuList(@RequestParam("ids") List<Long> ids) {
        roleWeixinMenuService.deleteRoleWeixinMenuListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信菜单和角色关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('system:role-weixin-menu:query')")
    public CommonResult<RoleWeixinMenuRespVO> getRoleWeixinMenu(@RequestParam("id") Long id) {
        RoleWeixinMenuDO roleWeixinMenu = roleWeixinMenuService.getRoleWeixinMenu(id);
        return success(BeanUtils.toBean(roleWeixinMenu, RoleWeixinMenuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信菜单和角色关联分页")
//    @PreAuthorize("@ss.hasPermission('system:role-weixin-menu:query')")
    public CommonResult<PageResult<RoleWeixinMenuRespVO>> getRoleWeixinMenuPage(@Valid RoleWeixinMenuPageReqVO pageReqVO) {
        PageResult<RoleWeixinMenuDO> pageResult = roleWeixinMenuService.getRoleWeixinMenuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RoleWeixinMenuRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得微信菜单和角色关联分页")
//    @PreAuthorize("@ss.hasPermission('system:role-weixin-menu:query')")
    public CommonResult<List<RoleWeixinMenuDO>> list(@Valid RoleWeixinMenuDO pageReqVO) {
        List<RoleWeixinMenuDO> list = roleWeixinMenuService.list(pageReqVO);
        return success(list);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出微信菜单和角色关联 Excel")
//    @PreAuthorize("@ss.hasPermission('system:role-weixin-menu:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRoleWeixinMenuExcel(@Valid RoleWeixinMenuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RoleWeixinMenuDO> list = roleWeixinMenuService.getRoleWeixinMenuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信菜单和角色关联.xls", "数据", RoleWeixinMenuRespVO.class,
                        BeanUtils.toBean(list, RoleWeixinMenuRespVO.class));
    }

}